extract: Warn when skipping an f-string with substitutions - #1343
Open
ColinHouse wants to merge 1 commit into
Open
extract: Warn when skipping an f-string with substitutions#1343ColinHouse wants to merge 1 commit into
ColinHouse wants to merge 1 commit into
Conversation
An f-string that interpolates values cannot be translated, so extraction skips it. Since python-babel#915 replaced eval with ast it is skipped silently: the message never reaches translators, and nothing tells the author. Warn instead, in the same format as the existing "Empty msgid" warning. Both tokenisation paths are covered: before Python 3.12 the whole f-string is a single STRING token, and from 3.12 (PEP 701) a substitution arrives as a non-FSTRING_* token and the f-string is dropped where current_fstring_start is reset. Fixes python-babel#715
ColinHouse
force-pushed
the
warn-skipped-fstring
branch
from
September 12, 2026 15:17
c7bb4c3 to
6f30522
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
_(f"Hello {name}")cannot be translated, so extraction skips it. Since #915replaced
evalwithast, it is skipped silently:pybabel extractexits 0,writes no message, and prints nothing. The string never reaches translators and
ships untranslated, with no signal to the author.
This warns instead, in the same format as the existing "Empty msgid" warning:
There are two code paths, because f-string tokenisation changed in 3.12:
STRINGtoken and_parse_python_stringreturns
None;FSTRING_*token and thef-string is dropped where
current_fstring_startis reset.Both are covered, and the suite was run on 3.11 and 3.12.
It warns rather than raising, as the issue body asks ("warn the user about the
incorrect usage instead of simply crashing"). Raising would break builds that
extract successfully today. Happy to make it an error if you prefer.
A constant f-string such as
_(f"spam" f"eggs")is still extracted and does notwarn; an f-string outside a translation call is untouched.
The stale
# TODO: we could raise an error or warning when not all nodes are constantsis replaced, since this implements it.fixes #715